#ifndef DATAGENERATOR_H
#define DATAGENERATOR_H

#include <QObject>
#include <QSqlDatabase>
#include <QString>
#include <QMap>
#include <QVariant>
#include <QRandomGenerator>

struct ColumnConfig {
    QString name;
    QString type;
    int minLength;
    int maxLength;
    QVariant minValue;
    QVariant maxValue;
    QString pattern;  // 用于生成特定格式的数据
    bool nullable;
    double nullProbability;  // 为NULL的概率(0.0-1.0)
    
    ColumnConfig() : minLength(0), maxLength(100), nullable(false), nullProbability(0.0) {}
};

struct TableConfig {
    QString name;
    int rowCount;
    QMap<QString, ColumnConfig> columns;
    
    TableConfig() : rowCount(100) {}
    TableConfig(const QString &n) : name(n), rowCount(100) {}
};

class DataGenerator : public QObject
{
    Q_OBJECT

public:
    explicit DataGenerator(QObject *parent = nullptr);
    
    void setDatabase(const QSqlDatabase &db);
    bool generateData(const TableConfig &config);
    QString getLastErrorMessage() const;
    
signals:
    void generationProgress(int percentage);
    void generationFinished(const QString &message);
    void generationError(const QString &error);

public slots:
    void cancelGeneration();

private:
    QVariant generateValue(const ColumnConfig &config);
    QString generateString(const ColumnConfig &config);
    int generateInt(const ColumnConfig &config);
    double generateDouble(const ColumnConfig &config);
    QVariant generateDate(const ColumnConfig &config);
    QVariant generateDateTime(const ColumnConfig &config);
    
    bool createTable(const TableConfig &config);
    bool insertData(const TableConfig &config);
    
    QSqlDatabase m_database;
    QString m_lastError;
    bool m_cancelRequested;
    QRandomGenerator m_random;
};

#endif // DATAGENERATOR_H